package com.example.cici.tryxutil3_httpandannotation;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.cici.tryxutil3_httpandannotation.util.CountDownTimerUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


@ContentView(R.layout.main_activity)
public class login extends AppCompatActivity{
    private String http = "http://119.23.26.217";
    private String phone_number;
    @ViewInject(R.id.get_code)
    private Button btn_get_code;
    @ViewInject(R.id.tip)
    private TextView tip;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
    }

    //获取验证码
    @Event(value = R.id.get_code)
    private void testOnclick(View view){

        EditText editText = (EditText) findViewById(R.id.username);
        phone_number = editText.getText().toString();
        Log.e("number",phone_number);
        //判断手机号是否为空
        if (phone_number.equals("")){
            tip.setText("手机号不能为空");
            tip.setTextColor(Color.rgb(255,0,0));
        }else if(validatePhoneNumber(phone_number)){
            tip.setText("手机号可用");
            tip.setTextColor(Color.rgb(0,255,0));

            //请求的地址
            RequestParams params = new RequestParams(http+"/users/sendsms");
            //请求的name和传的参数
            params.addBodyParameter("phone",phone_number);

            //post请求获取验证码
            x.http().post(params, new Callback.CacheCallback<String>() {
                //传值后回调的函数
                @Override
                public boolean onCache(String result) {
                    return false;
                }

                //传值成功时
                @Override
                public void onSuccess(String result) {
                    Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {
                    Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFinished() {
                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(btn_get_code,6000,1000);
                    countDownTimerUtils.start();
                }
            });
        }else {
            tip.setText("手机号格式不正确");
            tip.setTextColor(Color.rgb(255,0,0));

        }
    }
    @Event(R.id.login)
    private void login(View view){
        EditText editText = (EditText) findViewById(R.id.code);
        EditText editText1 = (EditText) findViewById(R.id.username);
        String code = editText.getText().toString();
        String phone= editText1.getText().toString();

        //判断验证码是否为空
        if(code.equals("")){
            tip.setText("验证码不能为空");
            tip.setTextColor(Color.rgb(255,0,0));
        }
        else{
            RequestParams params = new RequestParams(http+"/users/login");
            params.addBodyParameter("code",code);
            params.addBodyParameter("phone",phone);

            x.http().post(params, new Callback.CacheCallback<String>() {
                @Override
                public boolean onCache(String result) {
                    return false;
                }

                @Override
                public void onSuccess(String result) {

                    try {
                        JSONObject object = new JSONObject(result);
                        Log.e("result",result);
                        int err = object.getInt("error");
                        if (err!=0){
                            tip.setText("验证码不正确");
                            tip.setTextColor(Color.rgb(255,0,0));
                            Log.e("error","出错了！");
                        }
                        else{
                            Intent intent = new Intent(login.this,SecondActivity.class);
                            startActivity(intent);
                            Log.e("success","验证码正确");
                            String token = object.getString("token");
                            Log.e("token",token);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }

    //正则表达式验证手机号
    private boolean validatePhoneNumber(String phone_number){
        String telRegex = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147,145))\\d{8}$";
        //正则表达式的matches
        return phone_number.matches(telRegex);
    }
}

